package com.asiainfo.iov.service;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.text.TextUtils;

import com.asiainfo.iov.configs.ConstantsME;
import com.asiainfo.iov.configs.EventBusTag;
import com.asiainfo.iov.configs.InterfaceConfig;
import com.asiainfo.iov.entity.MessageInfo;
import com.asiainfo.iov.networkModel.HttpUtil;
import com.framework.utils.CollectionUtil;
import com.framework.utils.PreferencesHelper;

import org.greenrobot.eventbus.EventBus;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 消息通知service
 *
 * @author YobertJomi
 * className NotifyMessageService
 * created at  2017/4/6  13:38
 */

public class NotifyMessageService extends Service {

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
//            HttpUtil.getInstance().requestShopMessageList(null, InterfaceConfig.shopMessageList, 1, 10, new HttpUtil
//                    .OnRequestListResult<MessageInfo>() {
//
//                @Override
//                public void onSuccess(List<MessageInfo> list, String... msg) {
//                    boolean notifyNewMessage = false;
//                    if (!CollectionUtil.getInstance().isEmpty(list) && !list.get(0).getIsReaded()) {
//                        List<MessageInfo> listFromDB = initMessageListFromDB();
//                        if (!CollectionUtil.getInstance().isEmpty(listFromDB) && !listFromDB.get(0).getIsReaded()) {
//                            if (!TextUtils.equals(list.get(0).getId(), listFromDB.get(0).getId())) {
//                                notifyNewMessage = true;
//                            }
//                        } else {
//                            notifyNewMessage = true;
//                        }
//                    }
//                    PreferencesHelper.getInstance().putInfo(ConstantsME.hasNewMessage, notifyNewMessage);
//                    if (notifyNewMessage) {
//                        PreferencesHelper.getInstance().putInfo(ConstantsME.newMessage, list.get(0).getTitle() + ":"
//                                + list.get(0).getContent());
//                        EventBus.getDefault().post(list.get(0), EventBusTag.newMessage);
//                    }
//                }
//
//                @Override
//                public void onFail(int code, String msg) {
//
//                }
//            });
        }
    };
    private Timer timer;
    private TimerTask timerTask;

    @Override
    public void onCreate() {
        super.onCreate();
        initTimer();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    /**
     * 全局timer，用于检查新消息到达------未使用长链接
     */
    private void initTimer() {
        timer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(0);
            }
        };
        timer.schedule(timerTask, 10000, 300000);
    }

    @SuppressWarnings("unchecked")
//    private List<MessageInfo> initMessageListFromDB() {
//        return GreenDaoManager.getInstance().queryEntityList(getApplicationContext(), MessageInfo.class);
//    }

    @Override
    public void onDestroy() {
        cancelTask();
        super.onDestroy();
    }

    private void cancelTask() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (timerTask != null) {
            timerTask.cancel();
            timerTask = null;
        }
    }
}
